using System;
using System.Collections.Generic;
using System.Linq;

namespace TestConsole3
{
    /// <summary>
    /// 有序排列组合
    /// </summary>
    public class Order
    {
         //结果
        List<List<int>> result = new List<List<int>>();

        //自然数
        List<int> data = new List<int>() { 1, 2, 3, 4, 5 };

        //临时存放数据
        List<int> temp = new List<int>();


        public void Test()
        {
            for (int i = 0; i < data.Count; i++)
            {
                temp.Add(0);
            }

            int startNum = 1;
            //遍历所有长度的
            for (int k = 2; k < data.Count; k++)
            {
                if (startNum + k > data.Count) break;
                Console.WriteLine($"------------------------------len:{k}");
                RepeatNum(startNum, k);

            }

            //打印总长度
            Console.WriteLine(result.Count + 1);

            //查看所有排列
            foreach (var item in result)
            {
                foreach (var obj in item)
                {
                    Console.Write(obj + ",");
                }
                Console.WriteLine();
            }

        }

        /// <summary>
        /// 穷举递归 nx(n-2)矩阵
        /// </summary>
        /// <param name="constNum"></param>
        /// <param name="len"></param>
        public void RepeatNum(int constNum, int len)
        {
            if (len == 2)
            {
                GetNum(constNum);
            }
            else
            {
                for (int i = constNum; i < data.Count; i++)
                {

                    if (i + 1 < data.Count && (i - 1 + len) <= data.Count)
                    {
                        Console.WriteLine($"start:{data[i - 1]}");
                        temp[len - 1] = data[i - 1];
                        RepeatNum(i + 1, len - 1);

                    }
                }
            }
        }

        /// <summary>
        /// 递归每次都是2x2矩阵
        /// </summary>
        /// <param name="constNum"></param>
        public void GetNum(int constNum)
        {
            for (int F = constNum; F < data.Count; F++)
            {
                Console.WriteLine($"{data[constNum - 1]},{data[F]}");
                temp[0] = data[constNum - 1];
                temp[1] = data[F];
                var ele = temp.FindAll(x => x > 0).OrderBy(x => x).ToList();
                result.Add(ele);
            }
            if (constNum < data.Count) GetNum(constNum + 1);
        }
    }
}